# -*- coding: utf-8 -*-
import oss2
import os

class AliyunOSSUploader:
    def __init__(self):
        # 阿里云 OSS 访问凭证
        self.access_key_id = 'LTAI5tA4ZkpL6mCDjYj87L6J'
        self.access_key_secret = '95i6OPEJs9dBt1lyZYpSKDd3Ux73WA'
        self.endpoint = 'oss-cn-beijing.aliyuncs.com'
        self.bucket_name = 'yigonglibrarymanage'
        self.auth = oss2.Auth(self.access_key_id, self.access_key_secret)

    #方法返回图片资源在云服务器的url
    def upload_image(self, local_file_path, remote_file_name):
        bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket_name)

        try:
            bucket.put_object_from_file(remote_file_name, local_file_path)
            url = f"https://{self.bucket_name}.{self.endpoint}/{remote_file_name}"
            print(f"Successfully uploaded {local_file_path} to OSS bucket as {remote_file_name}")
            return url
        except oss2.exceptions.OssError as e:
            print(f"Failed to upload {local_file_path} to OSS bucket : {e}")
            return None


